<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="盘点序列号">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
        #sns{
            padding: 4px 0;
            margin-top: -1px;
            width:100%;height: 310px;font-size: 12px;color: #666;
            border:1px solid #dddddd;
        }

        #sns:focus{
            border:1px solid #0a60ff;
        }
    </style>

    <script>
        let houseId;
        //map : key-id   value-goodsSn

        jQuery(function () {
            houseId = "${house.id}";

            $("#houseName").html("${house.name}");
        });

        layui.use(['table','form','element'], function() {
            let table = layui.table,
                form = layui.form;
            table.render({
                id: 'table_result',
                elem: '#table_result',
                height: 350,
                limit: Number.MAX_VALUE,
                data: [],
                text: {
                    none: '无' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                },
                cols: [[
                    {type: 'numbers'},
                    {field: 'sn', minWidth: 120, title: '序列号', style: "font-size:12px;"},
                    {field: 'status', width: 90, title: '盘点状态', style: "font-size:12px;", templet: function (d) {
                        if (d.status == 1) {
                            return '<span style="color: #5FB878; font-size: 12px;">吻合</span>';
                        } else if (d.status == 2) {
                            return '<span style="color: #1E9FFF; font-size: 12px;">添加</span>';
                        } else if (d.status == 3) {
                            return '<span style="color: #FF5722; font-size: 12px;">删除</span>';
                        } else {
                            return '<span style="color: #FFB800; font-size: 12px;">重复</span>';
                        }
                    }},
                ]]
            });

            table.render({
                id: 'table',
                elem: '#table',
                height: 350,
                limit: Number.MAX_VALUE,
                data: [],
                text: {
                    none: '无' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                },
                cols: [[
                    {type: 'numbers'},
                    {field: 'sn', minWidth: 120, title: '账面序列号', style: "font-size:12px;"},
                ]]
            });
        });


        function  getMapLength(map) {
            return Object.keys(map).length;
        }

        function getBySn(sn, map) {
            let data = null;
            for (var key in map){
                if (map[key] != undefined && map[key].sn.indexOf(sn)!=-1) {
                    data = map[key];
                    delete data.LAY_TABLE_INDEX;
                    return data;
                }
            }
            return data;
        }

        let goods_snList = [];
        let del_sn_list = [];//被删除的sn
        let fit_sn_list = [];//吻合的sn
        let repeat_sn_list = [];//与其他仓库重复的sn
        let add_sn_list = [];//新增的sn
        let snMap = {};
        function setSnList(goodsSnList,fitGoodsSnList,delGoodsSnList,addGoodsSnList) {
            goods_snList = goodsSnList;
            fit_sn_list = fitGoodsSnList==undefined ? [] : fitGoodsSnList;
            del_sn_list = delGoodsSnList==undefined ? [] : delGoodsSnList;
            add_sn_list = addGoodsSnList==undefined ? [] : addGoodsSnList;

            let sns = '';
            for (let i=0;i<add_sn_list.length;i++) {
                let d=add_sn_list[i];
                sns += d.sn+"\n";
            }
            for (let i=0;i<fit_sn_list.length;i++) {
                let d=fit_sn_list[i];
                sns += d.sn+"\n";
            }
            $("#sns").val(sns);

            for (let i=0;i<goods_snList.length;i++) {
                snMap[goods_snList[i].sn] = goods_snList[i];
            }
            console.log("22222");
            setTimeout(function () {
                layui.table.reload("table", {data:goods_snList});
            },100);
            $("#stockNumber").html(goods_snList.length);
        }

    </script>

</html:header>
<body style="background-color: #ffffff; min-width: 530px;">
    <div id="snDiv">
        <div style="color: #999999;">
            [仓库：<span id="houseName" style="color: #FF5722;"></span>]
            <span style="padding-left: 10px;">库存数量 <span id="stockNumber" style="color:#FF5722">0</span></span>
        </div>
        <hr>

        <div style="overflow: hidden;">
            <div style="float: left;width: 200px;">
                <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>
            </div>

            <div style="float: left;width: 100px;height: 300px;text-align: center;">
                <div style="margin: 140px 0 20px 0px;">
                    <button id="copyAll" class="layui-btn layui-btn-sm layui-btn-primary">
                        复制全部>>
                    </button>
                </div>

                <script>
                    $("#copyAll").click(function () {
                        let str = "";
                        for (let i=0;i<goods_snList.length;i++) {
                            str += goods_snList[i].sn + "\n";
                        }
                        $("#sns").val(str);
                    });

                </script>
            </div>

            <div style="float: right;width: 200px; padding-right: 20px;height: 348px;margin-top: 10px;">
                <div style="width:100%;height: 38px;line-height: 38px;font-size: 14px;color:#666;background-color: #eeeeee; text-align: center;border:1px solid #dddddd;">
                    实际序列号
                </div>
                <textarea id="sns" placeholder="请录入或修改序列号，一条数据一行"></textarea>
            </div>
        </div>

        <hr style="margin-right: 20px;"/>
        <div style="overflow: hidden;">
            <div style="float: right; padding-right: 20px;">
                <a id="sure" class="layui-btn layui-btn-normal" lay-submit="" lay-filter="search">确认</a>
                <a id="backParent" class="layui-btn layui-btn-primary" lay-submit="" lay-filter="search">取消</a>
            </div>
        </div>

        <script>
            $("#sure").click(function () {
                let sns = $("#sns").val().split("\n");
                let input_sn_map = {};
                let temp_fit_map={};
                let temp_add_map={};
                for (let i=0;i<sns.length;i++) {
                    let sn = sns[i];
                    if (!emptyEntity(sn) && isNumberOrAlpha(sn)) {
                        input_sn_map[sn] = sn;
                        if (snMap[sn] == undefined) {
                            //将新增的sn放入临时map
                            temp_add_map[sn] = {sn:sn,goodsId:"${goodsId}",houseId:"${house.id}"};
                        } else {
                            //存储吻合的sn
                            temp_fit_map[sn] = snMap[sn];
                        }
                    }
                }
                fit_sn_list = genListFromMap(temp_fit_map);

                for (let i=0;i<goods_snList.length;i++) {
                    if (input_sn_map[goods_snList[i].sn] == undefined) {
                        //如果当前sn没有输入，说明是被删除的sn
                        del_sn_list.push(goods_snList[i]);
                    }
                }

                if (getMapLength(temp_add_map) == 0) {
                    repeat_sn_list = [];
                    add_sn_list = [];
                    openResultDialog();
                } else {
                    //去后台检查，目前统计的sn是否在其他仓库有在库的
                    post("${baseUrl}house/goodsSn/checkSnStatusOutHouseId.action?goodsId=${goodsId}&houseId=${house.id}", genSnsFromMap(temp_add_map), function (res) {
                        if (res.code == 1) {
                            //存储重复的sn
                            repeat_sn_list = res.data;

                            //去除重复的sn
                            removeByList(repeat_sn_list,temp_add_map);

                            //存储新增的sn
                            add_sn_list = genListFromMap(temp_add_map);

                            openResultDialog();
                        } else {
                            errTip(res.msg);
                        }
                    });
                }

            });

            $("#backParent").click(function () {
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            });
        </script>
    </div>
</body>
<html:loading/>

<div id="resultDialog" style="display: none;margin-top: 10px;">
    <div style="color: #666;font-size: 12px; padding-left: 16px;">
        实际序列号数量：
        <span id="total-num"></span>个，
        <span id="add-num"></span>个新增，
        <span id="del-num"></span>个删除，
        <span id="fit-num"></span>个吻合，
        <span id="repeat-num"></span>个重复。
    </div>
    <table class="layui-hide" id="table_result" lay-filter="table_result" lay-size="sm"></table>
    <hr>
    <div style="overflow: hidden;">
        <div style="float: right; padding-right: 16px;">
            <button id="change" class="layui-btn layui-btn-primary">继续修改</button>
            <button id="enter" class="layui-btn layui-btn-normal">确认无误</button>
        </div>
    </div>
    <script>
        $("#change").click(function () {
            closeAll();
        });

        $("#enter").click(function () {
            window.parent.processCheckHouseSn(add_sn_list, del_sn_list, fit_sn_list);
            closeAll();

            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });
    </script>
</div>



<script>
    function genListFromMap(map) {
        let list=[];
        //获取已选择的数据
        for (var key in map){
            if (map[key] != undefined) {
                list.push(map[key]);
            }
        }
        return list;
    }

    function removeByList(list,map) {
        $.each(list, function (i,d) {
            delete map[d.sn];
        });
        return map;
    }

    function genMapFromList(list) {
        let map = {};
        $.each(list, function (i,d) {
            map[d.sn] = d;
        });
        return map;
    }
    
    function genSnsFromMap(map) {
        let list=[];
        //获取已选择的数据
        for (var key in map){
            if (map[key] != undefined) {
                list.push(map[key].sn);
            }
        }
        return list;
    }

    function openResultDialog() {
        var index = layer.open({
            title:  '盘点结果',
            type: 1,
            shade: 0.2,
            shadeClose: false,
            area: ['400px', '100%'],
            content: $("#resultDialog"),
            success: function(layero, index){
                let list = [];
                for (let i=0;i<add_sn_list.length;i++) {
                    let d=add_sn_list[i];
                    d.status = 2;
                    list.push(d);
                }
                for (let i=0;i<del_sn_list.length;i++) {
                    let d=del_sn_list[i];
                    d.status = 3;
                    list.push(d);
                }
                for (let i=0;i<repeat_sn_list.length;i++) {
                    let d=repeat_sn_list[i];
                    d.status = 4;
                    list.push(d);
                }
                for (let i=0;i<fit_sn_list.length;i++) {
                    let d=fit_sn_list[i];
                    d.status = 1;
                    list.push(d);
                }
                let total = fit_sn_list.length + add_sn_list.length;
                $("#total-num").html(total);
                $("#add-num").html(add_sn_list.length);
                $("#del-num").html(del_sn_list.length);
                $("#repeat-num").html(repeat_sn_list.length);
                $("#fit-num").html(fit_sn_list.length);
                layui.table.reload('table_result', {data:list});
            }
        });
    }
</script>
</html>